// platform.ts - 统一平台工具模块
// @/utils/monitor/utils/platform.ts

/**
 * 获取平台类型
 */
export function getPlatform(): string {
  // #ifdef H5
  return 'h5';
  // #endif
  
  // #ifdef MP-WEIXIN
  return 'weixin';
  // #endif
  
  // #ifdef MP-ALIPAY
  return 'alipay';
  // #endif
  
  // #ifdef MP-BAIDU
  return 'baidu';
  // #endif
  
  // #ifdef MP-TOUTIAO
  return 'toutiao';
  // #endif
  
  // #ifdef MP-QQ
  return 'qq';
  // #endif
  
  // #ifdef APP-PLUS
  return 'app';
  // #endif
  
  return 'unknown';
}

/**
 * 是否是H5平台
 */
export function isH5(): boolean {
  return getPlatform() === 'h5';
}

/**
 * 是否是微信小程序
 */
export function isWeixin(): boolean {
  return getPlatform() === 'weixin';
}

/**
 * 是否是支付宝小程序
 */
export function isAlipay(): boolean {
  return getPlatform() === 'alipay';
}

/**
 * 是否是App平台
 */
export function isApp(): boolean {
  return getPlatform() === 'app';
}

/**
 * 是否是小程序平台
 */
export function isMiniProgram(): boolean {
  const platform = getPlatform();
  return ['weixin', 'alipay', 'baidu', 'toutiao', 'qq'].includes(platform);
}

/**
 * 获取系统信息
 */
export function getSystemInfo(): UniApp.GetSystemInfoResult {
  try {
    return uni.getSystemInfoSync();
  } catch (error) {
    console.warn('[Platform] Failed to get system info:', error);
    return {} as UniApp.GetSystemInfoResult;
  }
}

/**
 * 获取状态栏高度
 */
export function getStatusBarHeight(): number {
  const systemInfo = getSystemInfo();
  return systemInfo.statusBarHeight || 0;
}

/**
 * 获取导航栏高度
 */
export function getNavBarHeight(): number {
  const statusBarHeight = getStatusBarHeight();
  
  // #ifdef MP-WEIXIN
  try {
    const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
    return (menuButtonInfo.top - statusBarHeight) * 2 + menuButtonInfo.height + statusBarHeight;
  } catch (error) {
    return statusBarHeight + 44;
  }
  // #endif
  
  // #ifndef MP-WEIXIN
  return statusBarHeight + 44;
  // #endif
}

/**
 * 获取安全区域
 */
export function getSafeArea(): { 
  top: number; 
  bottom: number; 
  left: number; 
  right: number; 
} {
  const systemInfo = getSystemInfo();
  const safeArea = systemInfo.safeArea;
  
  if (safeArea) {
    return {
      top: safeArea.top,
      bottom: systemInfo.screenHeight - safeArea.bottom,
      left: safeArea.left,
      right: systemInfo.screenWidth - safeArea.right
    };
  }
  
  // 降级方案
  return {
    top: getStatusBarHeight(),
    bottom: 0,
    left: 0,
    right: 0
  };
}

/**
 * px转rpx
 */
export function px2rpx(px: number): number {
  const systemInfo = getSystemInfo();
  const windowWidth = systemInfo.windowWidth || 375;
  return px * 750 / windowWidth;
}

/**
 * rpx转px
 */
export function rpx2px(rpx: number): number {
  const systemInfo = getSystemInfo();
  const windowWidth = systemInfo.windowWidth || 375;
  return rpx * windowWidth / 750;
}

/**
 * 获取屏幕信息
 */
export function getScreenInfo(): {
  width: number;
  height: number;
  pixelRatio: number;
  windowWidth: number;
  windowHeight: number;
} {
  const systemInfo = getSystemInfo();
  return {
    width: systemInfo.screenWidth || 0,
    height: systemInfo.screenHeight || 0,
    pixelRatio: systemInfo.pixelRatio || 1,
    windowWidth: systemInfo.windowWidth || 0,
    windowHeight: systemInfo.windowHeight || 0
  };
}

/**
 * 获取设备信息
 */
export function getDeviceInfo(): {
  brand: string;
  model: string;
  system: string;
  platform: string;
} {
  const systemInfo = getSystemInfo();
  return {
    brand: systemInfo.brand || 'unknown',
    model: systemInfo.model || 'unknown', 
    system: systemInfo.system || 'unknown',
    platform: getPlatform()
  };
}

/**
 * 检查是否支持某个API
 */
export function checkApiSupport(apiName: keyof UniNamespace.Uni): boolean {
  try {
    return typeof uni[apiName] === 'function';
  } catch (error) {
    return false;
  }
}

/**
 * 获取网络类型
 */
export function getNetworkType(): Promise<string> {
  return new Promise((resolve) => {
    // #ifdef H5
    if (typeof navigator !== 'undefined' && navigator.connection) {
      const connection = (navigator as any).connection;
      resolve(connection.effectiveType || 'unknown');
      return;
    }
    // #endif
    
    if (checkApiSupport('getNetworkType')) {
      uni.getNetworkType({
        success: (res) => {
          resolve(res.networkType);
        },
        fail: () => {
          resolve('unknown');
        }
      });
    } else {
      resolve('unknown');
    }
  });
}

/**
 * 平台特性检测
 */
export const platformFeatures = {
  // 是否支持分享
  supportShare: () => checkApiSupport('share'),
  // 是否支持支付
  supportPay: () => checkApiSupport('requestPayment'),
  // 是否支持扫码
  supportScanCode: () => checkApiSupport('scanCode'),
  // 是否支持位置
  supportLocation: () => checkApiSupport('getLocation'),
  // 是否支持相机
  supportCamera: () => checkApiSupport('chooseImage'),
  // 是否支持文件
  supportFile: () => checkApiSupport('chooseFile')
}; 